package tspda

import aps.RequestInstance
import com.thoughtworks.xstream.XStream
import glory.xxd.ConsumerUtil
import glory.xxd.ProducerUtil
import glory.xxd.Utils
import glory.xxd.XmlUtils
import org.json.JSONObject
import org.json.XML
import tsCIM.modelForInterface.RequestModel
import tsCIM.modelForInterface.ResultModel

/**
 * 消息数据解析
 */
class CommentService {


    def senQueueName = "jms.queue.APS.EAPADMIN"
    def replyQueueName = "jms.queue.APS.EAPADMIN.REPLY"
    public final static Map map = new HashMap();
    /**
     * 获取发送topic接收的queue消息 返回json
     * @param logUUID
     * @param header 消息头
     * @param body 消息体
     * @return body内的内容 json格式
     */
    def getJsonByObjectForTopic2Queue(logUUID, header, body) {

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "-200")
        standardJSONForPDA.put("message", "消息通信异常!")
        standardJSONForPDA.put("data", null)

        XStream xstream = new XStream();
        xstream.alias("Request", RequestInstance.class)
        xstream.alias("Body", body.getClass())
        def requestInstance = new RequestInstance()
        requestInstance.setBody(body)
        requestInstance.setHeader(header)

        def xml = xstream.toXML(requestInstance)

        def msgState = ProducerUtil.sendTopicMsg(logUUID, "jms.topic.APS.Client2EAP", xml)

        if ("200".equals(msgState)) {
            def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, "jms.queue.EAP.REPLY.Client")
            if (xmlReturn) {
                xmlReturn = xmlReturn[0]
                def data = Utils.getJsonObjectByXML(xmlReturn)
                //这边的判断值是针对showMessage返回，其他接口可以不要
                if (data.has("MESSAGEINFO")) {
                    standardJSONForPDA.put("message", data.getString("MESSAGEINFO"))
                } else {
                    if (data.has("MESSAGE")) {
                        standardJSONForPDA.put("message", data.getString("MESSAGE"))
                    }
                    standardJSONForPDA.put("data", data)
                    standardJSONForPDA.put("code", 200)


                }
            } else {
                standardJSONForPDA.put("message", "接收EAP消息超时!")
            }
        } else {
            standardJSONForPDA.put("message", "发送请求异常!")
        }
        return Utils.getFormatString(standardJSONForPDA)
    }

    /**
     * 2019-03-06 测试json-api
     * @param logUUID
     * @param header
     * @param body
     * @return
     */
    def getJsonByObjectForTopic2Queue_1(logUUID, header, body) {

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "-200")
        standardJSONForPDA.put("message", "消息通信异常!")
        standardJSONForPDA.put("data", null)

        XStream xstream = new XStream();
        xstream.alias("Request", RequestInstance.class)
        xstream.alias("Body", body.getClass())
        def requestInstance = new RequestInstance()
        requestInstance.setBody(body)
        requestInstance.setHeader(header)

        def xml = xstream.toXML(requestInstance)

        def msgState = ProducerUtil.sendTopicMsg(logUUID, "jms.topic.APS.Client2EAP", xml)

        if ("200".equals(msgState)) {
            def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, "jms.queue.EAP.REPLY.Client")
            if (xmlReturn) {
                xmlReturn = xmlReturn[0]
                //2019-03-06 测试json-api
                def data = Utils.getJsonObjectByXML_1(xmlReturn)
                if (data.has("MESSAGEINFO")) {
                    standardJSONForPDA.put("message", data.getString("MESSAGEINFO"))
                } else {
                    if (data.has("MESSAGE")) {
                        standardJSONForPDA.put("message", data.getString("MESSAGE"))
                    }
                    standardJSONForPDA.put("data", data)
                    standardJSONForPDA.put("code", 200)

                }
            } else {
                standardJSONForPDA.put("message", "接收EAP消息超时!")
            }
        } else {
            standardJSONForPDA.put("message", "发送请求异常!")
        }
        return Utils.getFormatString(standardJSONForPDA)
    }

    /**
     * 2019-03-11 测试json-api
     * @param logUUID
     * @param header
     * @param body
     * @Autor
     * @return
     */
    def getJsonByObjectForTopic2Queue_2(logUUID, header, body) {

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "-200")
        standardJSONForPDA.put("message", "消息通信异常!")
        standardJSONForPDA.put("data", null)

        XStream xstream = new XStream();
        xstream.alias("Request", RequestInstance.class)
        xstream.alias("Body", body.getClass())
        def requestInstance = new RequestInstance()
        requestInstance.setBody(body)
        requestInstance.setHeader(header)

        def xml = xstream.toXML(requestInstance)

        def msgState = ProducerUtil.sendTopicMsg(logUUID, "jms.topic.APS.Client2EAP", xml)

        if ("200".equals(msgState)) {
            def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, "jms.queue.EAP.REPLY.Client")
            if (xmlReturn) {
                xmlReturn = xmlReturn[0]
                //2019-03-11 测试json-api
                def data = Utils.getJsonObjectByXML_2(xmlReturn)
                standardJSONForPDA.put("data", data)
                standardJSONForPDA.put("code", 200)
            } else {
                standardJSONForPDA.put("message", "接收EAP消息超时!")
            }
        } else {
            standardJSONForPDA.put("message", "发送请求异常!")
        }
        return Utils.getFormatString(standardJSONForPDA)
    }
    /**
     * 获取发送queue接收的queue消息 返回json
     * @param logUUID
     * @param header 消息头
     * @param body 消息体
     * @return body内的内容 json格式
     */
    def getJsonByObjectForQueue2Queue(logUUID, header, body) {

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "-200")
        standardJSONForPDA.put("message", "消息通信异常")
        standardJSONForPDA.put("data", null)

        XStream xstream = new XStream();
        xstream.alias("Request", RequestInstance.class)
        xstream.alias("Body", body.getClass())
        def requestInstance = new RequestInstance()
        requestInstance.setBody(body)
        requestInstance.setHeader(header)
        def xml = xstream.toXML(requestInstance)

        def code = ProducerUtil.sendQueueMsg(logUUID, senQueueName, xml)

        if ("200".equals(code)) {
            def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, replyQueueName)
            if (xmlReturn) {
                xmlReturn = xmlReturn[0]
                def data = Utils.getJsonObjectByXML(xmlReturn)

                if (data.has("MESSAGEINFO")) {
                    standardJSONForPDA.put("message", data?.getString("MESSAGEINFO"))
                } else {
                    if (data.has("MESSAGE")) {
                        standardJSONForPDA.put("message", data.getString("MESSAGE"))
                    }
                    standardJSONForPDA.put("data", data)
                    standardJSONForPDA.put("code", 200)
                }
            } else {
                standardJSONForPDA.put("message", "接收EAP消息超时!")
            }
        } else {
            standardJSONForPDA.put("message", "发送请求异常!")
        }
        return Utils.getFormatString(standardJSONForPDA)
    }


    def getJsonByObjectForQueue2Queue(logUUID, allJSON) {

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "-200")
        standardJSONForPDA.put("message", "消息通信异常")
        standardJSONForPDA.put("data", null)

        def xml = XML.toString(allJSON)

        def code = ProducerUtil.sendQueueMsg(logUUID, senQueueName, xml)

        if ("200".equals(code)) {
            def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, replyQueueName)
            if (xmlReturn) {
                xmlReturn = xmlReturn[0]
                def data = Utils.getJsonObjectByXML(xmlReturn)

                if (data.has("MESSAGEINFO")) {
                    standardJSONForPDA.put("message", data.getString("MESSAGEINFO"))
                } else {
                    if (data.has("MESSAGE")) {
                        standardJSONForPDA.put("message", data.getString("MESSAGE"))
                    }
                    standardJSONForPDA.put("data", data)
                    standardJSONForPDA.put("code", 200)
                }
            } else {
                standardJSONForPDA.put("message", "接收EAP消息超时!")
            }
        } else {
            standardJSONForPDA.put("message", "发送请求异常!")
        }
        return Utils.getFormatString(standardJSONForPDA)
    }

    /**
     *
     * @param logUUID messageID
     * @param xml 消息内容xml格式
     * @return body内的内容 json格式
     */
    def getJsonByObjectForTopic2Queue(logUUID, xml) {

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "-200")
        standardJSONForPDA.put("message", "消息通信异常")
        standardJSONForPDA.put("data", null)

        def msgState = ProducerUtil.sendTopicMsg(logUUID, "jms.topic.APS.Client2EAP", xml)

        if ("200".equals(msgState)) {
            def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, "jms.queue.EAP.REPLY.Client")
            if (xmlReturn) {
                xmlReturn = xmlReturn[0]
                def data = Utils.getJsonObjectByXML(xmlReturn)
                if (data.has("MESSAGEINFO")) {
                    standardJSONForPDA.put("message", data.getString("MESSAGEINFO"))
                } else {
                    if (data.has("MESSAGE")) {
                        standardJSONForPDA.put("message", data.getString("MESSAGE"))
                    }
                    standardJSONForPDA.put("data", data)
                    standardJSONForPDA.put("code", 200)
                }
            } else {
                standardJSONForPDA.put("message", "接收EAP消息超时!")
            }
        } else {
            standardJSONForPDA.put("message", "发送请求异常!")
        }
        return Utils.getFormatString(standardJSONForPDA)
    }

    /**
     *
     * @param logUUID
     * @param allJSON
     * @return
     */
    def getJsonByJsonForTopic2Queue(logUUID, allJSON) {

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "-200")
        standardJSONForPDA.put("message", "消息通信异常")
        standardJSONForPDA.put("data", null)

        def xml = XML.toString(allJSON)

        def msgState = ProducerUtil.sendTopicMsg(logUUID, "jms.topic.APS.Client2EAP", xml)

        if ("200".equals(msgState)) {
            def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, "jms.queue.EAP.REPLY.Client")
            if (xmlReturn) {
                xmlReturn = xmlReturn[0]
                def data = Utils.getJsonObjectByXML(xmlReturn)

                if (data.has("MESSAGEINFO")) {
                    standardJSONForPDA.put("message", data.getString("MESSAGEINFO"))
                } else {
                    if (data.has("MESSAGE")) {
                        standardJSONForPDA.put("message", data.getString("MESSAGE"))
                    }
                    standardJSONForPDA.put("data", data)
                    standardJSONForPDA.put("code", 200)
                }
            } else {
                standardJSONForPDA.put("message", "接收EAP消息超时!")
            }
        } else {
            standardJSONForPDA.put("message", "发送请求异常!")
        }
        return Utils.getFormatString(standardJSONForPDA)

    }

    /**
     * 获取queue消息
     */
    def getQueueMsg(logUUID, msgName) {

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "-200")
        standardJSONForPDA.put("message", "消息通信异常!")
        standardJSONForPDA.put("data", null)

        def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, msgName)
        if (xmlReturn) {
            xmlReturn = xmlReturn[0]
            def data = Utils.getJsonObjectByXML(xmlReturn)
            if (data.has("MESSAGEINFO")) {
                standardJSONForPDA.put("message", data.getString("MESSAGEINFO"))
            } else {
                if (data.has("MESSAGE")) {
                    standardJSONForPDA.put("message", data.getString("MESSAGE"))
                }
                standardJSONForPDA.put("data", data)
                standardJSONForPDA.put("code", 200)
            }
        } else {
            standardJSONForPDA.put("message", "接收EAP消息超时!")
        }
        return Utils.getFormatString(standardJSONForPDA)
    }


    /**
     * 获取queue消息
     * 2019-03-06 测试JSON API 使用测试方法： getJsonObjectByXML_1
     */
    def getQueueMsg_1(logUUID, msgName) {

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "-200")
        standardJSONForPDA.put("message", "消息通信异常!")
        standardJSONForPDA.put("data", null)

        def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, msgName)
        if (xmlReturn) {
            xmlReturn = xmlReturn[0]
            //2019-03-06 测试JSON API 使用测试方法： getJsonObjectByXML_1
            def data = Utils.getJsonObjectByXML_1(xmlReturn)

            if (data.has("MESSAGEINFO")) {
                standardJSONForPDA.put("message", data.getString("MESSAGEINFO"))
            } else {
                if (data.has("MESSAGE")) {
                    standardJSONForPDA.put("message", data.getString("MESSAGE"))
                }
                standardJSONForPDA.put("data", data)
                standardJSONForPDA.put("code", 200)
            }
        } else {
            standardJSONForPDA.put("message", "接收EAP消息超时!")
        }
        return Utils.getFormatString(standardJSONForPDA)
    }



    /**
     * @auth glory.xxd 2019-03-16
     * 与MQ通信 发送topic消息 接收 queue 消息
     * @param requestModel
     * @param sendMQMsgName
     * @param returnMQMsgName
     * @param sendTest 发送测试 true /false
     * @param returnTest 返回测试 为false、null 时，则走MQ，为xml时，则不走mq
     * @return
     */
    def getJsonByTopic2Queue(RequestModel requestModel,sendMQMsgName,returnMQMsgName ,sendTest,returnTest) {

        def result = ResultModel.getResultModel()

        def msgId = requestModel.getHeader().getTRANSACTIONID()
        def sendXml = XmlUtils.parseObject2Xml(requestModel.getRequestModel())
        print sendXml
        def requestJson = ProducerUtil.sendTopicMsg(msgId,sendMQMsgName, sendXml,sendTest)

        //判定发送结果如果大于0标识发送成功，则进入if块，接收消息
        if (requestJson.getInt(ResultModel.Code)>= 0 ) {
            def returnJson = ConsumerUtil.getQueueMsg(msgId, returnMQMsgName,returnTest)
            //判定接收状态：如果大于0，标识接收成功
            if (returnJson.getInt(ResultModel.Code)>= 0) {
                def xmlReturn = returnJson.getString(ResultModel.Data)
                //2019-03-06 测试json-api

                def data = Utils.Xml2Json(xmlReturn)

                //判定xml转换状态，如果结果标识有server状态，标识存在异常
                if( data.containsKey(ResultModel.ServerErrorFlag) && data.getBoolean(ResultModel.ServerErrorFlag)){
                    result = data
                }else{
                    result.put(ResultModel.Data,data)
                }
            } else {
                result = returnJson
            }
        }else{
            result = requestJson
        }
        return result
    }

    /**
     * @auth glory.xxd 2019-03-28
     * 与MQ通信 发送topic消息 接收 queue 消息  针对INPUTDATALIST  特殊处理
     * @param requestModel
     * @param sendMQMsgName
     * @param returnMQMsgName
     * @param sendTest 发送测试 true /false
     * @param returnTest 返回测试 为false、null 时，则走MQ，为xml时，则不走mq
     * @return
     */
    def getJsonByTopic2Queue_1(RequestModel requestModel,sendMQMsgName,returnMQMsgName ,sendTest,returnTest) {

        def result = ResultModel.getResultModel()

        def msgId = requestModel.getHeader().getTRANSACTIONID()

        def sendXml = XmlUtils.json2xml(requestModel.getRequestModel())
        print sendXml
        def requestJson = ProducerUtil.sendTopicMsg(msgId,sendMQMsgName, sendXml,sendTest)

        //判定发送结果如果大于0标识发送成功，则进入if块，接收消息
        if (requestJson.getInt(ResultModel.Code)>= 0 ) {
            def returnJson = ConsumerUtil.getQueueMsg(msgId, returnMQMsgName,returnTest)
            //判定接收状态：如果大于0，标识接收成功
            if (returnJson.getInt(ResultModel.Code)>= 0) {
                def xmlReturn = returnJson.getString(ResultModel.Data)
                //2019-03-06 测试json-api
                def data = Utils.Xml2Json(xmlReturn)
                //判定xml转换状态，如果结果标识有server状态，标识存在异常
                if( data.containsKey(ResultModel.ServerErrorFlag) && data.getBoolean(ResultModel.ServerErrorFlag)){
                    result = data
                }else{
                    result.put(ResultModel.Data,data)
                }
            } else {
                result = returnJson
            }
        }else{
            result = requestJson
        }
        return result
    }
}